using System;
using System.IO;
using System.Xml.Serialization;

namespace XML;

internal class XMLFile
{
	public static bool Save<T>(string filename, T c)
	{
		Stream stream = null;
		try
		{
			stream = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None);
		}
		catch (Exception)
		{
			return false;
		}
		try
		{
			XmlSerializer f = new XmlSerializer(typeof(T));
			f.Serialize(stream, c);
		}
		catch
		{
			stream.Close();
			return false;
		}
		stream.Close();
		return true;
	}

	public static bool Open<T>(string FileName, ref T c)
	{
		if (!File.Exists(FileName))
		{
			return false;
		}
		Stream stream = null;
		try
		{
			stream = new FileStream(FileName, FileMode.Open);
		}
		catch
		{
			return false;
		}
		try
		{
			XmlSerializer deserializer = new XmlSerializer(typeof(T));
			object obj = deserializer.Deserialize(stream);
			c = (T)obj;
		}
		catch
		{
			stream.Close();
			return false;
		}
		stream.Close();
		return true;
	}

	public static bool Open<T>(Stream stream, ref T c)
	{
		try
		{
			XmlSerializer deserializer = new XmlSerializer(typeof(T));
			object obj = deserializer.Deserialize(stream);
			c = (T)obj;
		}
		catch
		{
			stream.Close();
			return false;
		}
		stream.Close();
		return true;
	}
}
